<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('云产品自动部署信息列表')" />
</head>
<body class="gray-bg" style="background-color: #ffffff;">
    <div class="container-div" style="padding: 5px 20px;">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" name="cloudProduct" value="oss"/>
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>云厂商：</label>
                                <select name="cloudVendor" th:with="type=${@dict.getType('cloud_vendor')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>证书域名：</label>
                                <input type="text" name="certificateDomain"/>
                            </li>
                            <li class="select-time">
                                <label>自动续签过期时间：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginExpiredTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endExpiredTime]"/>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="status" th:with="type=${@dict.getType('sys_yes_no')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="addOSS()" shiro:hasPermission="SSL:autoDeploy:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="SSL:autoDeploy:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="SSL:autoDeploy:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>
            <div class="col-sm-12 select-table table-bordered">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('SSL:autoDeploy:edit')}]];
        var removeFlag = [[${@permission.hasPermi('SSL:autoDeploy:remove')}]];
        var cloudVendorDatas = [[${@dict.getType('cloud_vendor')}]];
        var cloudProductDatas = [[${@dict.getType('cloud_product')}]];
        var statusDatas = [[${@dict.getType('sys_yes_no')}]];
        var prefix = ctx + "SSL/autoDeploy";

        function addOSS(){
            window.parent.addOSS()
        }

        //刷新数据的方法
        function refreshFun(){
            $.table.refresh();
        }

        function deploySsl(id, domainTxt){
            $.modal.confirm("确认要立即部署"+domainTxt+"的证书吗？", function() {
                $.operate.get(ctx + "SSL/autoDeployIndex/autoDeploy/"+id);
            })
        }

        function getDaysBetweenDates(date) {
            // 将日期字符串转换为Date对象
            var d1 = new Date(date);
            var d2 = new Date();
            // 计算两个日期的时间差（毫秒）
            var timeDiff = Math.abs(d1.getTime()-d2.getTime());
            // 计算天数
            var days = Math.ceil(timeDiff / (1000 * 3600 * 24));
            return days;
        }

        function isAutoSslTools(row) {
            if (row.isAutoSsl === 'N' || !row.isAutoSsl) {
                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="editIsAutoSSL(\'' + row.id + '\', \'Y\')"></i> ';
            } else if(row.isAutoSsl === 'Y'){
                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="editIsAutoSSL(\'' + row.id + '\', \'N\')"></i> ';
            }
        }

        function editIsAutoSSL(id, isSSL) {
            $.modal.confirm("确认要"+(isSSL === 'Y'?"开启":"关闭")+"自动部署、续签吗？", function() {
                $.operate.post(prefix + "/edit", { "id": id, "isAutoSsl": isSSL });
            })
        }

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "云产品自动部署信息",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'Id',
                    visible: false
                },
                {
                    field: 'cloudVendor',
                    title: '云厂商',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(cloudVendorDatas, value);
                    }
                },
                {
                    field: 'cloudProduct',
                    title: '云产品',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(cloudProductDatas, value);
                    }
                },
                {
                    field: 'certificateDomain',
                    title: '产品域名'
                },
                {
                    field: 'isUniversal',
                    title: '证书类型',
                    formatter: function(value, row, index) {
                        if(value === 'Y'){
                            return '<p><span class="badge badge-primary">泛域名</span></p>';
                        }else{
                            return '<p><span class="badge badge-info">普通域名</span></p>';
                        }
                    }
                },
                {
                    field: 'domainTxt',
                    title: '绑定域名证书'
                },
                {
                    field: 'expiredTime',
                    title: '证书过期时间',
                    formatter:function (value, row, index){
                        var tmpHtml="";
                        if(value){
                            value = getDaysBetweenDates(value);
                            if(value > 30){
                                tmpHtml= '<span class="badge badge-success">剩余：'+value+' 天</span>';
                            }else if (value <=30 && value > 20){
                                tmpHtml= '<span class="badge badge-primary">剩余：'+value+' 天</span>';
                            }else if (value <=20 && value > 10){
                                tmpHtml= '<span class="badge badge-warning">剩余：'+value+' 天</span>';
                            }else if(value > 0 && value<=10){
                                tmpHtml= '<span class="badge badge-danger">剩余：'+value+' 天</span>';
                            }else{
                                tmpHtml= '<span class="badge badge-danger">已过期</span>';
                            }
                        }else{
                            tmpHtml= '<span class="badge badge-danger">未知</span>';
                        }
                        return '<p>'+tmpHtml+'</p>';
                    }
                },
                {
                    field: 'isAutoSsl',
                    title: '自动部署、续签',
                    formatter: function(value, row, index) {
                        return isAutoSslTools(row);
                    }
                },
                {
                    field: 'autoExpiredTime',
                    title: '自动续签过期时间',
                    formatter:function (value, row, index){
                        var tmpHtml="";
                        if(value){
                            value = getDaysBetweenDates(value);
                            if(value > 30){
                                tmpHtml= '<span class="badge badge-success">剩余：'+value+' 天</span>';
                            }else if (value <=30 && value > 20){
                                tmpHtml= '<span class="badge badge-primary">剩余：'+value+' 天</span>';
                            }else if (value <=20 && value > 10){
                                tmpHtml= '<span class="badge badge-warning">剩余：'+value+' 天</span>';
                            }else if(value > 0 && value<=10){
                                tmpHtml= '<span class="badge badge-danger">剩余：'+value+' 天</span>';
                            }else{
                                tmpHtml= '<span class="badge badge-danger">已过期'+value+' 天</span>';
                            }
                        }else{
                            tmpHtml= '<span class="badge badge-danger">未知</span>';
                        }
                        return '<p>'+tmpHtml+'<span style="margin-left: 5px;cursor: pointer;" onclick="window.parent.toSetTimePage(\'' + row.id + '\')" class="badge badge-info">设置</span></p>';
                    }
                },
                {
                    field: 'deployStatus',
                    title: '部署状态',
                    formatter: function(value, row, index) {
                        if(value === 'Y'){
                            return "<span class='badge badge-primary'>已部署成功</span>";
                        }else if(value === 'N' || !value){
                            return '<span class="badge badge-danger">未部署</span><span style="margin-left: 5px;cursor: pointer;" onClick="deploySsl(\'' + row.id + '\', \''+row.certificateDomain+'\')" class="badge badge-info">点击立即部署</span>';
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
    </script>
</body>
</html>
